<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click.native="DelClick(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="query.page"
                :page-sizes="[2, 5, 7, 10]"
                :page-size="query.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹框 -->
      <el-dialog
        :title="isEdit ? '编辑角色' : '新增角色'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hClose"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="hSubmit"
              >确定</el-button
            >
            <el-button size="small" @click="showDialog = false">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoles, DeleteRoles, AddRoles, updateRole } from "@/api/settings";
export default {
  name: "settings",

  data() {
    return {
      query: {
        page: 1, //页码
        pagesize: 7, //每页条数
      },
      list: [], //保存角色列表
      total: 0, //总条数
      roleForm: {
        name: "",
        description: "",
      },
      showDialog: false,
      isEdit: false, //false 新增 true 编辑
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "角色描述不能为空", trigger: "blur" },
        ],
      },
    };
  },

  created() {
    this.loadRoles();
  },
  methods: {
    //弹层发生变化时触发
    hClose() {
      this.$refs.roleForm.resetFields();
      this.roleForm = {};
    },
    //每页条数
    handleSizeChange(val) {
      //当页码条数发生变化时，跳到第一页
      this.query.page = 1;
      this.query.pagesize = val;
      //刷新页面
      this.loadRoles();
    },
    // 当前页码
    handleCurrentChange(val) {
      this.query.page = val;
      //刷新页面
      this.loadRoles();
    },
    //获取角色列表
    async loadRoles() {
      try {
        const { data: res } = await getRoles(this.query);
        this.list = res.rows;
        this.total = res.total;
      } catch (error) {}
    },
    //删除角色
    async DelRoles(id) {
      try {
        await DeleteRoles(id);
        this.$message.success("删除成功");
        if (this.list.length === 1) {
          this.query.page--;
          if (this.query.page <= 0) {
            this.query.page = 1;
          }
        }
        //刷新页面
        this.loadRoles();
      } catch (error) {}
    },
    //删除按钮
    DelClick(id) {
      this.$confirm("确定要删除此角色吗？, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.DelRoles(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //新增角色
    async HaddRoles() {
      try {
        await AddRoles(this.roleForm);
        //添加成功，关闭弹框 刷新页面
        this.showDialog = false;
        this.$message.success("新增成功");
        this.total++;
        const maxPage = Math.ceil(this.total / this.query.pagesize);
        this.query.page = maxPage;
        //刷新页面
        this.loadRoles();
      } catch (error) {}
    },
    //更新角色
    async doEdit() {
      try {
        await updateRole(this.roleForm);
        this.showDialog = false;
        this.$message.success("编辑成功");
        //刷新页面
        this.loadRoles();
      } catch (error) {}
    },
    //确定
    hSubmit() {
      this.$refs.roleForm.validate((valid) => {
        if (!valid) return; //效验不通过
        this.isEdit ? this.doEdit() : this.HaddRoles();
      });
    },
    //新增按钮
    hAdd() {
      this.showDialog = true;
      this.isEdit = false;
    },
    //编辑按钮
    hEdit(row) {
      this.showDialog = true;
      this.isEdit = true;
      this.roleForm = { ...row };
    },
  },
};
</script>

<style lang="scss" scoped>
</style>